package o2o.business.sms;


import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.client.sms.AliSmsClient;
import o2o.cache.sys.SysCustomerCache;
import o2o.dao.PO.sys.SysCustomerInfo;
import o2o.task.sys.SmsLogUtils;

import java.util.regex.Pattern;

//统一调用微服务
public class JYSmsSeverBusiness {

    public static void sendSMS(String sysAppKey, String phone, String type) {
        sendSMS(sysAppKey, null, phone, type, null, null);
    }

    public static void sendSMS(String sysAppKey, String areaCode, String phone, String type, String smsCode, String ip) {

        if (ValidateUtil.isNull(phone) || ValidateUtil.isNull(type)) {
            return;
        }
        if (ValidateUtil.isNull(sysAppKey)) {
            sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
        }

        SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
        if (null == customerInfo) {
            return;
        }
        String smsKey = customerInfo.getSmsKey();
        String smsProduct = customerInfo.getSmsProduct();

        //判断是国内手机号还是国外手机号
        if (!isChinesePhone(phone)) {
            //国外手机号
            smsKey = customerInfo.getSmsKeyOut();
            smsProduct = customerInfo.getSmsProductOut();
        }

        if (ValidateUtil.isNull(areaCode)) {
            //判断是国内手机号还是国外手机号
            if (!isChinesePhone(phone)) {
                //国外手机号
                smsKey = customerInfo.getSmsKeyOut();
                smsProduct = customerInfo.getSmsProductOut();
            }
        } else {
            if ("86".equals(areaCode) || "+86".equals(areaCode)) {
                // 国内的
            } else {
                //国外手机号
                smsKey = customerInfo.getSmsKeyOut();
                smsProduct = customerInfo.getSmsProductOut();

                if ("81".equals(areaCode)) {
                    // 日本手机号是11位
                    // 日本的不处理
                } else {
                    // 大于11位的，将开头去掉
                    if (phone.length() > 11) {
                        int len = phone.length() - 11;
                        phone = phone.substring(len, phone.length());
                    }
                }

            }
        }

        AliSmsClient aliSmsClient = AliSmsClient.getInstance().setSysCustomer(smsKey).setType(type).setPhone(phone).setIp(ip);
        if (null == aliSmsClient) {
            return;
        }
        if (ValidateUtil.isNotNull(customerInfo.getSmsUrl())) {
            aliSmsClient.setUrl(customerInfo.getSmsUrl());
        }

        if (ValidateUtil.isNotNull(smsCode)) {
            aliSmsClient.setParam("code", smsCode);
        }
        if (ValidateUtil.isNotNull(smsProduct)) {
            aliSmsClient.setParam("product", smsProduct);
        }

        Long time = DateUtils.getCurrTime();
        System.out.println("发送短信：" + DateUtils.getCurrentDateTimeMills() + "," + customerInfo.getSmsUrl() + "," + smsKey + "," + type + "," + smsProduct + "," + phone + ":" + smsCode);

        aliSmsClient.sendMsg();

        // 存mongodb
        SmsLogUtils.addLog(phone, smsCode, time, smsKey);
    }


    //大陆手机号  不以0开头，并且是11位
    public static boolean isChinesePhone(String str) {
        String regex = "^[1]\\d{10}$";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(str).matches();
    }

}
